package com.bright.cwduijie.duijie.util;

import cn.hutool.core.util.ReUtil;
import com.bright.cwduijie.common.util.StringUtil;
import com.bright.cwduijie.duijie.pojo.query.DuijieQuery;

import java.time.YearMonth;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Author hxj
 * @Date 2024/7/14 13:20
 * @Description
 */
public class CommonUtil {

    // 根据对接年份和月份构造yearMonth 注：允许只传年份和年份月份必传 若没传年份 则月份不起效
    public static String getSelectedYearMonth(Integer years, Integer months) {
        String selectedYearMonth = null;
        if (years != null) {
            selectedYearMonth = "" + years;
            if (months != null) {
                selectedYearMonth += months >= 10 ? months : "0" + months;
            }
        }
        return selectedYearMonth;
    }

    public static YearMonth getYearMonthFromKJQJString(String kjqj) {
        if (kjqj == null) return null;
        String yearString = StringUtil.substring(kjqj, 0, 4);
        String monthString = StringUtil.substring(kjqj, 5);

        return YearMonth.of(Integer.parseInt(yearString), Integer.parseInt(monthString));
    }

    // 用正则表达式获取“当前启用的会计期间是[xxxx年xx月]”的这个年月
    private static final Pattern yearMonthPattern = Pattern.compile("当前启用的会计期间是\\[(\\d{4})年(\\d+?)月\\]");
    public static YearMonth getEnableYearMonth(String writeLog) {
        String yearString = ReUtil.get(yearMonthPattern, writeLog, 1);
        String monthString = ReUtil.get(yearMonthPattern, writeLog, 2);

        return YearMonth.of(Integer.parseInt(yearString), Integer.parseInt(monthString));
    }

    // 用正则表达式获取“资产编码[xxx]在统一社会信用代码为[N24401117219426129]的组织内重复”的这个资产编码
    private static final Pattern fixedAssetRepeatCodePattern = Pattern.compile("资产编码\\[(\\S+?)\\]在统一社会信用代码为\\S+?的组织内重复");
    public static List<String> getRepeatAssetCode(String writeLog) {
        return ReUtil.getAllGroups(fixedAssetRepeatCodePattern, writeLog, false, true);
    }

    private static final Pattern usedVoucherDataKeyPattern = Pattern.compile("该科目已被会计凭证关联使用：\\[(\\S+?)\\]");
    public static List<String> getAllVoucherDataKey(String writeLog) {
        return ReUtil.getAllGroups(usedVoucherDataKeyPattern, writeLog, false, true);
    }

    // 根据凭证的dataKey获取会计期间 e.g.:202401
    private static final Pattern kjqjOfVoucherWriteLogPattern = Pattern.compile("该科目已被会计凭证关联使用：\\[\\S+?-(20\\d\\d[01]\\d)-\\S+?\\]");
    public static List<String> getKJQJFromVoucherWriteLog(String writeLog) {
        return ReUtil.getAllGroups(kjqjOfVoucherWriteLogPattern, writeLog, false, true);
    }

    private static final Pattern kjqjOfVoucherDataKeyPattern = Pattern.compile("\\S+?-(20\\d\\d[01]\\d)-\\S+?");
    public static String getKJQJFromVoucherDataKey(String voucherDataKey) {
        return ReUtil.get(kjqjOfVoucherDataKeyPattern, voucherDataKey, 1);
    }
}
